package design;



import com.uniq.db.ConnectionFactory;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;


public class Purchase extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JLabel lblNewLabel_1;
	private JTextField textField_3;
	private JLabel lblSize;
	private JLabel lblColor;
	private JLabel lblArticleNo;
	private JLabel lblCostprice;
	private JLabel lblQuantity;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	String qrtext=new String();
	private String id;
	private String supplier;
	private String brand;
	private String size;
	private String color;
	private String article_no;
	private String selling_price;
	private JButton btnSell;
	private JLabel lblNewLabel_2;
	private JTextArea textArea;
	private JTextField textField_8;

	public static void main(final String args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Purchase frame = new Purchase(args);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Purchase(String qrtext) {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 772, 634);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		for (String text:qrtext.split(","))
		{
			if(text.split(":")[0].equals("id"))
			{
				id=text.split(":")[1];
			}
			else if(text.split(":")[0].equals("supplier"))
			{
				supplier=text.split(":")[1];
			}
			else if(text.split(":")[0].equals("brand"))
			{
				brand=text.split(":")[1];
			}
			else if(text.split(":")[0].equals("size"))
			{
				size=text.split(":")[1];
			}
			
			else if(text.split(":")[0].equals("color"))
			{
				color=text.split(":")[1];
			}
			
			else if(text.split(":")[0].equals("article_no"))
			{
				article_no=text.split(":")[1];
			}
			
			else if(text.split(":")[0].equals("selling_price"))
			{
				selling_price=text.split(":")[1];
			}
			
		}
		
		JLabel lblId = new JLabel("id");
		lblId.setBounds(47, 53, 46, 14);
		contentPane.add(lblId);
		
		textField = new JTextField();
		textField.setBounds(213, 50, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Supplier");
		lblNewLabel.setBounds(47, 96, 62, 14);
		contentPane.add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setBounds(213, 93, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(213, 137, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		lblNewLabel_1 = new JLabel("Brand");
		lblNewLabel_1.setBounds(47, 140, 46, 14);
		contentPane.add(lblNewLabel_1);
		
		textField_3 = new JTextField();
		textField_3.setBounds(213, 178, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		lblSize = new JLabel("Size");
		lblSize.setBounds(47, 181, 46, 14);
		contentPane.add(lblSize);
		
		lblColor = new JLabel("Color");
		lblColor.setBounds(47, 229, 46, 14);
		contentPane.add(lblColor);
		
		lblArticleNo = new JLabel("Article No");
		lblArticleNo.setBounds(47, 273, 78, 14);
		contentPane.add(lblArticleNo);
		
		lblCostprice = new JLabel("Selling_price");
		lblCostprice.setBounds(47, 318, 86, 14);
		contentPane.add(lblCostprice);
		
		lblQuantity = new JLabel("Quantity");
		lblQuantity.setBounds(47, 365, 86, 14);
		contentPane.add(lblQuantity);
		
		textField_4 = new JTextField();
		textField_4.setBounds(213, 226, 86, 20);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(213, 270, 86, 20);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		textField_6 = new JTextField();
		textField_6.setBounds(213, 315, 86, 20);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		textField_7 = new JTextField();
		textField_7.setBounds(213, 362, 86, 20);
		contentPane.add(textField_7);
		textField_7.setColumns(10);
		
		textField.setText(id);
		textField_1.setText(supplier);
		textField_2.setText(brand);
		textField_3.setText(size);
		textField_4.setText(color);
		textField_5.setText(article_no);
		textField_6.setText(selling_price);
		
		btnSell = new JButton("Sell");
		btnSell.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
                            insertValues();
                            
				updateValues();
			}

		});
		btnSell.setBounds(347, 566, 89, 23);
		contentPane.add(btnSell);
		
		lblNewLabel_2 = new JLabel("Delievery address");
		lblNewLabel_2.setBounds(46, 455, 98, 14);
		contentPane.add(lblNewLabel_2);
		
		textArea = new JTextArea();
		textArea.setBounds(213, 460, 153, 64);
		contentPane.add(textArea);
		
		textField_8 = new JTextField();
		textField_8.setBounds(213, 408, 138, 20);
		contentPane.add(textField_8);
		textField_8.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(47, 411, 62, 14);
		contentPane.add(lblEmail);
		
		
	}
        void insertValues()
        {
        try {
            Connection con=ConnectionFactory.getConnection();
            Statement st=con.createStatement();
           String id=textField.getText();
           String email=textField_8.getText();
           String quantity=textField_7.getText();
          String selling_price=textField_6.getText();
          String delievery=textArea.getText();
 
           double price=(Integer.parseInt(quantity))* (Double.parseDouble(selling_price));
           
           
            String sql="insert into iorder(customername,product,quantity,price,delivery,o_date) values('"+email+"','"+id+"','"+quantity+"','"+price+"','"+delievery+"',(SELECT CURDATE()))";
            st.execute(sql);
            
            }
        
        catch (SQLException ex) {
            Logger.getLogger(Purchase.class.getName()).log(Level.SEVERE, null, ex);
        }

            
            
        }

	protected void updateValues() 
	{
		 try
         {
			 String product=textField.getText();
			 String quantity=textField_7.getText();
String query="Select * from stock where id="+product;

Connection con=ConnectionFactory.getConnection();
Statement st=con.createStatement();
int src_quantity=0;
ResultSet rs=st.executeQuery(query);
if(rs.next())
{
src_quantity=rs.getInt("stock_count");
}
String queryString="update stock set stock_count="+(src_quantity-Integer.parseInt(quantity))+" where id="+product;
Statement st1=con.createStatement();
st1.executeUpdate(queryString);
JOptionPane.showMessageDialog(rootPane, "Success");

 }
catch(Exception e)
         {
e.printStackTrace();
         }

		
	}
}
